//
//  MapViewController.m
//  ForeverLove
//
//  Created by 王维斌 on 15/11/16.
//  Copyright © 2015年 文萱竹. All rights reserved.
//

#import "MapViewController.h"
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
#import <AVOSCloud/AVOSCloud.h>
#import "Test.h"
@interface MapViewController ()<MKMapViewDelegate,CLLocationManagerDelegate>
{
    CLGeocoder * _geocoder;
}
@property (nonatomic, strong) NSMutableArray *allUsersArray;
@property (nonatomic, strong) NSMutableArray *arrayUseranme;
@property (strong, nonatomic) IBOutlet MKMapView *mapView;
@property(nonatomic,strong) CLLocationManager * locationManager;

@end

@implementation MapViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //1.跟踪用户位置
    self.mapView.userTrackingMode = MKUserTrackingModeFollowWithHeading;
    //2.设置地图显示类型(立体.平面)
    self.mapView.mapType = MKMapTypeStandard;
    //3.在地图上不断更新我们的位置(通过代理协议)设置地图显示代理(有代理来完成地图上位置的跟新)
    self.mapView.delegate = self;
    
    self.locationManager = [[CLLocationManager alloc] init];
    _locationManager.delegate = self;
    
    //提示需要权限
    [_locationManager requestAlwaysAuthorization];
    [_locationManager requestWhenInUseAuthorization];
    
    //设置定位管理精度
    _locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;
    //开启定位
    [_locationManager startUpdatingLocation];
    //实例化geo
    _geocoder = [[CLGeocoder alloc] init];
    
    self.mapView.userTrackingMode = MKUserTrackingModeFollow;
    
    UITapGestureRecognizer *tpGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapMapView:)];
    [self.mapView addGestureRecognizer:tpGR];
    
    //计算北京的经纬度
    //    [_geocoder geocodeAddressString:@"北京" completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
    //        //placemarks 地点,地标
    //        CLPlacemark * placemark = placemarks[0];
    //        NSLog(@"+++++%lf,%@",placemark.location.coordinate.longitude,placemark.country);
    //    }];
    
}

-(void)tapMapView:(UIGestureRecognizer *)tap
{
    self.arrayUseranme = [NSMutableArray array];
    //1.获取用户点击在mapView上的位置tap.view与self.mapview一样
    CGPoint point = [tap locationInView:self.mapView];
    //2.将数学坐标系中的点转换成经纬度坐标
    CLLocationCoordinate2D coordinate = [self.mapView convertPoint:point toCoordinateFromView:self.mapView];
    //3.创建大头针模型对象
    Test *customAnno = [[Test alloc] init];
#pragma mark---coordinate属性必须要赋值
    //必须要实现的属性
    customAnno.coordinate = coordinate;
    customAnno.title = @"他的位置";
    customAnno.imageName = @"pin_red@2x.png";
    [self.mapView addAnnotation:customAnno];
    //将大头针添加到mapView上
    
    AVObject *post = [AVObject objectWithClassName:@"Another"];
    [post setObject:[NSNumber numberWithDouble:coordinate.latitude] forKey:@"latitude"];
    [post setObject:[NSNumber numberWithDouble:coordinate.longitude] forKey:@"longitude"];
    [post saveInBackground];

    CLLocationCoordinate2D coors[2] = {0};
    
    AVQuery *query = [AVQuery queryWithClassName:@"Another"];
    
    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        self.allUsersArray = (NSMutableArray *)objects;
        
        for (AVUser * user  in objects) {
            
            [self.arrayUseranme addObject:user[@"latitude"]];
            [self.arrayUseranme addObject:user[@"longitude"]];
            
        }
        NSLog(@"%@----",self.arrayUseranme);
        
        //        [self.searchArray addObjectsFromArray:self.arrayUseranme];
        //
        //        [self.tableView  reloadData];
    }];
    
}





#pragma mark---实现MKMapViewDelegate
//实时跟新位置在地图上的位置
-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
    userLocation.title = @"您所在的位置";
    
    //根据当前用户获取中心点
    CLLocationCoordinate2D center = userLocation.location.coordinate;
    //设置地图的显示范围
    //设置经纬度跨度,跨度越小越清楚,越大越不清楚
    MKCoordinateSpan span = MKCoordinateSpanMake(0.1, 0.1);
    //设置显示区域
    //以中心点和给定的范围
    MKCoordinateRegion region = MKCoordinateRegionMake(center, span);
    [self.mapView setRegion:region animated:YES];
    
    
    AVObject *post = [AVObject objectWithClassName:@"Me"];
    
    [post setObject:[NSNumber numberWithDouble:center.latitude] forKey:@"latitude"];
    [post setObject:[NSNumber numberWithDouble:center.longitude] forKey:@"longitude"];
    [post saveInBackground];
    
}





@end
